//
//  TradeDetailVC.swift
//  LeGou
//
//  Created by lxm on 2022/8/1.
//

import UIKit

class IntegralExchangeRecord: ZMViewController, UITableViewDelegate, UITableViewDataSource, ListRequestObjct {
    //MARK: --------------------属性--------------------
    let padding : CGFloat = 15
    var pm = 0
    var listData = [ListModel]()
    var page = 1
    var pageSize = 10
    var refreshScrollView : UIScrollView {
        tableView
    }
    
    // 1 落锤次数 2额度
    var type = 0
    //MARK: --------------------UI控件--------------------
    let tableView = UITableView(frame: CGRect.zero, style: .plain).then { t in
        t.separatorStyle = .none
        t.estimatedRowHeight = 68
    }

    //MARK: --------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
        tableView.mj_header?.beginRefreshing()
    }
    
    //MARK: --------------------UI--------------------
    func setUI() {
        title = "兑换记录"
        view.addSubview(tableView)
        tableView.delegate = self
        tableView.dataSource = self
        
        tableView.tableFooterView = UIView()
        tableView.backgroundColor = "#F5F5F5".zm.hex()
        view.backgroundColor = "#F5F5F5".zm.hex()

        tableView.zm.register(nibName: "TradeDetalCell", reuserId: "TradeDetalCell")
        tableView.contentInset = UIEdgeInsets(top: 16, left: 0, bottom: 0, right: 0)

        addRefresh(scrollView: tableView) { [weak self] refresh in
            guard let self = self else {return}
            if refresh {
                self.page = 1
            }
            self.loadList()
        }
        addNodataScrollView(scrollView: tableView)
        
        tableView.mj_header?.ignoredScrollViewContentInsetTop = 16
        
        emptyImg = "nodata"
    }
    
    func addConstraints() {
        tableView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(padding)
            make.right.equalToSuperview().offset(-padding)
            make.top.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
 
    }
    
    func loadList() {
        let pa = [
            "page" : page,
            "limit" : pageSize,
        ] as [String : Any]
        if type == 1 {
            ZMUtil.listRequest(type: ListModel.self, api: VIPMallAPI.bidExchangeRecord(pa)) { resp in
                self.listRequest(resp)
            }
        } else {
            ZMUtil.listRequest(type: ListModel.self, api: VIPMallAPI.quotaExchangeRecord(pa)) { resp in
                self.listRequest(resp)
            }
        }
        
    }

    //MARK: --------------------delegate--------------------
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        tableView.mj_footer?.isHidden = listData.count == 0
        return listData.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TradeDetalCell") as! TradeDetalCell
        let data = listData[indexPath.row]
        cell.name.text = data.title
        cell.time.text = data.createTime
        cell.money.text = "+\(data.number.zm.toString(keep: 1))"
        cell.money.textColor = Style.mainColor
        cell.rightSub.isHidden = false
        cell.rightSub.text = "消耗积分：\(data.integral.zm.toString(keep: 4))"
        cell.mark.text = ""
        cell.setStyle(s: 0)
//        cell.
        return cell
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let allCount = listData.count
        let current = indexPath.row
        if allCount == 1 {
            ZMUtil.clipCircle(view: cell, Corner: [.topLeft, .topRight, .bottomLeft, .bottomRight], radius: 12)
        } else {
            if current == 0 {
                ZMUtil.clipCircle(view: cell, Corner: [.topLeft, .topRight,], radius: 12)
            } else if current == allCount - 1 {
                ZMUtil.clipCircle(view: cell, Corner: [.bottomLeft, .bottomRight], radius: 12)
            } else {
                cell.layer.mask = nil
            }
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
}


extension IntegralExchangeRecord {
    struct ListModel : HandyJSON {
        var createTime = ""
        var integral = 0.0
        var number = 0.0
        var title = ""
    }
}
